const puppeteer = require("puppeteer");

(async () => {
	// 启动浏览器
	const browser = await puppeteer.launch({
		headless: false, // 设置为 false 以便浏览器界面可见
		defaultViewport: null,
		args: [
			"--start-maximized", // 启动时最大化窗口
		],
	});

	// 设置视口为屏幕尺寸
	const page = await browser.newPage();

	page.on("response", async (response) => {
		// 检查响应的URL是否是特定接口的URL

		if (response.url().includes("/api/basic/LoginController/login")) {
			// 这里处理特定接口的回调
			console.log("特定接口的响应数据:", await response.json());
		}
	});

	// // 获取屏幕尺寸
	// const { width, height } = await page.evaluate(() => {
	// 	return {
	// 		width: window.screen.width,
	// 		height: window.screen.height - 200,
	// 	};
	// });
	// // 设置视口为屏幕尺寸
	// await page.setViewport({ width, height });
	// 导航到网页
	await page.goto("http://172.16.1.138:3100/");

	await handleLogin(page);
})();

async function handleLogin(page) {
	await page.waitForFunction(
		() => document.title.includes("登录"),
		{} // 可以传入选项，例如 { timeout: 10000 } 来设置超时时间
	);
	await page.type("#account", "jhejhe");
	await page.type("#password", "8888a8888@");
	await page.click("button");

	await handleSearch(page);
}

async function handleSearch(page) {
	await page.waitForFunction(
		() => document.title.includes("应用列表"),
		{} // 可以传入选项，例如 { timeout: 10000 } 来设置超时时间
	);
	await page.type("#appName input", "196地质大队");

	await page.click("button[text='查询']");

	setTimeout(async () => {
		await page.click("button[text='重置']");
		setTimeout(async () => {
			await handleAdd(page);
		}, 1000);
	}, 1000);
}

async function handleAdd(page) {
	await page.goto("http://172.16.1.138:3100/#/Configuration/baseMap");

	await page.waitForFunction(
		() => document.title.includes("底图配置"),
		{} // 可以传入选项，例如 { timeout: 10000 } 来设置超时时间
	);

	// await page.waitForSelector("#mapName", { visible: true });
	// await page.click("#addDom");

	await page.waitForSelector("#mapName");

	await page.type("#mapName input", "testMap");
	await page.type("#mapUrl input", "test");

	// 使用 $$ 方法和 'button' 选择器来获取所有的button元素
	const buttons = await page.$$("button");

	for (let button of buttons) {
		const span = await button.$("span");
		// console.log("span", span);
		const spanText = await page.evaluate(
			(element) => element.textContent,
			span
		);
		// console.log("text", text);

		if (spanText === "确 认") {
			await button.click();
			// setTimeout(async () => {
			// 	await handleRemove(page);
			// }, 2000);
		}
	}
}

async function handleRemove(page) {
	const trs = await page.$$("tr");
	// console.log("trs", trs.length);
	for (let tr of trs) {
		let tds = await tr.$$("td");
		// console.log("div", tds.length);

		for (let td of tds) {
			let div = await td.$("div");
			const divText = await td.evaluate((element) => element.textContent, div);
			// console.log("divText", divText);
			if (divText === "testMap") {
				// console.log("div", divText);
				let deleteTds = await tr.$$("td");
				for (let deleteTd of deleteTds) {
					let deleteDiv = await deleteTd.$("div");

					const deleteDivText = await deleteTd.evaluate(
						(element) => element.textContent,
						deleteDiv
					);
					// console.log("deleteD11ivText", deleteDivText);
					if (deleteDivText === "编辑删除") {
						const as = await deleteDiv.$$("a");
						for (let a of as) {
							const aText = await page.evaluate(
								(element) => element.textContent,
								a
							);

							if (aText === "删除") {
								await a.click();

								const xpathExpression = "//span[contains(text(), '确 定')]";
								await page.waitForXPath(xpathExpression);
								const data = await page.$x(xpathExpression);

								const elementHandle = data[0];
								if (elementHandle) {
									await elementHandle.click();
								}
							}
						}
					}
				}
			}
		}
	}
}
